<?php
// +----------------------------------------------------------------------
// | Yzncms [ 御宅男工作室 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2018 http://yzncms.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 御宅男 <530765310@qq.com>
// +----------------------------------------------------------------------

// +----------------------------------------------------------------------
// | 后台欢迎页
// +----------------------------------------------------------------------
namespace app\admin\controller;

use app\common\controller\Adminbase;
use think\Db;
use think\facade\Session;

class Main extends Adminbase
{
    protected $noNeedRight = ['index'];

    public function initialize()
    {
        $this->where = array();
        if (Session::get("admin.roleid") == 3) {
            $admin_where[] = ['area_id', '=', Session::get("admin.area_id")];
            $admin_where[] = ['roleid', '=', 4];
            $ying_ids=Db::name('admin')->where($admin_where)->column('id');
            $business_ids = Db::name('business')
                ->where('belong_admin_id', 'in',$ying_ids)
                ->column('id');
            $this->where[] = ['business_id', 'in', $business_ids];
        }
        if (Session::get("admin.roleid") == 4) {
            $business_ids = Db::name('business')->where('belong_admin_id', Session::get("admin.id"))->column('id');
            $this->where[] = ['business_id', 'in', $business_ids];
        }
        return parent::initialize(); // TODO: Change the autogenerated stub
    }

    //欢迎首页
    public function index()
    {
        if (IS_ROOT && $this->auth->password == encrypt_password('admin', $this->auth->encrypt)) {
            $this->assign('default_pass', 1);
        }
        $this->assign('sys_info', $this->get_sys_info());
        return $this->fetch('main/index2');
    }


    //phpinfo信息 按需显示在前台
    protected function get_sys_info()
    {
        //$sys_info['os'] = PHP_OS; //操作系统
        $sys_info['ip'] = GetHostByName($_SERVER['SERVER_NAME']); //服务器IP
        $sys_info['web_server'] = $_SERVER['SERVER_SOFTWARE']; //服务器环境
        $sys_info['phpv'] = phpversion(); //php版本
        $sys_info['fileupload'] = @ini_get('file_uploads') ? ini_get('upload_max_filesize') : 'unknown'; //文件上传限制
        $sys_info['memory_limit'] = ini_get('memory_limit'); //最大占用内存
        //$sys_info['set_time_limit'] = function_exists("set_time_limit") ? true : false; //最大执行时间
        //$sys_info['zlib'] = function_exists('gzclose') ? 'YES' : 'NO'; //Zlib支持
        //$sys_info['safe_mode'] = (boolean) ini_get('safe_mode') ? 'YES' : 'NO'; //安全模式
        //$sys_info['timezone'] = function_exists("date_default_timezone_get") ? date_default_timezone_get() : "no_timezone";
        //$sys_info['curl'] = function_exists('curl_init') ? 'YES' : 'NO'; //Curl支持
        //$sys_info['max_ex_time'] = @ini_get("max_execution_time") . 's';
        $sys_info['domain'] = $_SERVER['HTTP_HOST']; //域名
        $sys_info['remaining_space'] = function_exists('disk_free_space') ? round((disk_free_space(".") / (1024 * 1024)), 2) . 'M' : '未知'; //剩余空间
        //$sys_info['user_ip'] = $_SERVER['REMOTE_ADDR']; //用户IP地址
        $sys_info['beijing_time'] = gmdate("Y年n月j日 H:i:s", time() + 8 * 3600); //北京时间
        $sys_info['time'] = date("Y年n月j日 H:i:s"); //服务器时间
        //$sys_info['web_directory'] = $_SERVER["DOCUMENT_ROOT"]; //网站目录
        $mysqlinfo = Db::query("SELECT VERSION() as version");
        $sys_info['mysql_version'] = $mysqlinfo[0]['version'];
        if (function_exists("gd_info")) {
            //GD库版本
            $gd = gd_info();
            $sys_info['gdinfo'] = $gd['GD Version'];
        } else {
            $sys_info['gdinfo'] = "未知";
        }
        return $sys_info;
    }

    //index2 入驻服务站数量
    public function index1()
    {
        $where = array();
        $business_num = Db::name('business')
            ->where($where)
            ->where('status', 1)
            ->count();
        msg(0, '获取成功', $business_num);
    }

    //待审核服务站数量
    public function index2()
    {
        $business_num = Db::name('business')
            ->where('status', 0)
            ->count();
        msg(0, '获取成功', $business_num);
    }

    //用户数量
    public function index3()
    {
        $member = Db::name('member')->count();
        msg(0, '获取成功', $member);
    }

    //会员订单量 已完成
    public function orderNum()
    {
        $order_num = Db::name('order')->count();
        msg(0, '获取成功', $order_num);
    }

    //创建列表数据
    public function index4()
    {
        //服务站订单待发货 配送中 已完成 总金额 昨日金额  '0待支付 1待发货 2已完成 3待收货 配送中', status
        $data1 = Db::name('business_order')->where($this->where)->where('status', 1)->count();
        $data2 = Db::name('business_order')->where($this->where)->where('status', 3)->count();
        $data3 = Db::name('business_order')->where($this->where)->where('status', 2)->count();
        $data4 = Db::name('business_order')->where($this->where)->where('status', 2)->sum('price');
        $timescope = getTimestampScope(-1);
        $where[] = ['pay_time', 'between', [$timescope[0], $timescope[1]]];
        $data5 = Db::name('business_order')->where($where)->where('status', 2)->sum('price');
        $data['data1'] = $data1;
        $data['data2'] = $data2;
        $data['data3'] = $data3;
        $data['data4'] = $data4;
        $data['data5'] = $data5;
        msg(0, '', $data);
    }

    //获取饼状图信息
    public function pieCate()
    {
        //订单完成 todo 关联管理员
        $data = array();
        $goods_cate = Db::name('goods_categoty')->where('is_del', 0)->select();
        foreach ($goods_cate as $k => $v) {
            $goods_ids = Db::name('goods')
                ->where('cate_id', $v['id'])
                ->column('id');
            $goods_num = Db::name('business_order_detail')
                ->where('goods_id', 'in', $goods_ids)
                ->sum('goods_num');
            $data[] = ['value' => $goods_num, 'name' => $v['name']];
        }
        msg(0, '', $data);
    }

    public function barData()
    {
        $daysAgo = [];
        $price = [];
        $where[] = ['status', '=', 2];
        for ($i = 7; $i >= 1; $i--) {
            $daysAgo[] = date('d日', strtotime(-$i . " days"));
            $timescope = getTimestamp(-$i);
            $where[] = ['create_time', 'between', [$timescope[0], $timescope[1]]];
            $price[] = Db::name('business_order')->where($where)->where($this->where)->sum('price');
        }
        $data['date'] = $daysAgo;
        $data['price'] = $price;
        msg(0, '', $data);
    }

    //获取服务站订单排行
    public function businessOrder()
    {
        $where = array();
        //'0待支付 1待发货 2已完成 3待收货 配送中'
        $where[] = ['bo.status', '=', 2];
        $order_data = Db::name('business_order')
            ->alias('bo')
            ->join('business b', 'bo.business_id=b.id')
            ->where($where)
            ->where($this->where)
            ->group('bo.business_id')
            ->order('order_num')
            ->limit(10)
            ->field('b.business_name,count(*) as order_num')
            ->select();
        $data['business_name'] = [];
        $data['order_num'] = [];
        if ($order_data) {
            foreach ($order_data as $k => $v) {
                $data['business_name'][] = $v['business_name'];
                $data['order_num'][] = $v['order_num'];
            }
        } else {
            $business_info = Db::name('business')->limit(10)->select();
            foreach ($business_info as $k => $v) {
                $data['business_name'][] = $v['business_name'];
                $data['order_num'][] = 0;
            }
        }
        msg(0, '', $data);
    }
}
